package cn.edu.ujn.lab3.service.impl;

import cn.edu.ujn.lab3.dao.Orders;
import cn.edu.ujn.lab3.dao.OrdersMapper;
import cn.edu.ujn.lab3.service.OrdersService;
import cn.edu.ujn.lab3.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("ordersService")
@Transactional
public class OrdersServiceImpl implements OrdersService {

	@Autowired
	private OrdersMapper ordersDao;

	// 查询订单列表
	@Override
	public Page<Orders> findOrdersList(Integer page, Orders orders) {
		orders.setStart((page - 1) * orders.getRows());
		List<Orders> ordersList = ordersDao.selectOrdersList(orders);
		Integer ordersListCount = ordersDao.selectOrdersListCount(orders);
		//System.out.println(ordersList.get(0).toString());
		Page<Orders> ordersPage = new Page<Orders>();
		ordersPage.setPage(page);
		ordersPage.setRows(ordersList);
		ordersPage.setSize(orders.getRows());
		ordersPage.setTotal(ordersListCount);
		return ordersPage;
	}

	// 创建订单
	@Override
	public int createOrders(Orders orders) {
		System.out.println(orders);
		return ordersDao.insert(orders);
	}
	//根据id查询订单
	@Override
	public Orders getOrdersById(Integer id) {
		return ordersDao.selectByPrimaryKey(id);
	}
	//更新订单信息
	@Override
	public int updateOrders(Orders orders) {
		return ordersDao.updateByPrimaryKey(orders);
	}
	//删除订单
    @Override
    public int deleteOrders(Integer id) {
        return ordersDao.deleteByPrimaryKey(id);
    }

}